package bb.lanxing.activity.team;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import java.util.ArrayList;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.adapter.team.TeamApprovalAdapter;
import bb.lanxing.model.database.EventApproveUser;
import bb.lanxing.network.BiCiCallback;
import bb.lanxing.network.MyHttpClient;

public class TeamApprovalActivity extends BaseActivity {
    private final int APPROVAL_STATUS = 2;
    private final int REJECTED_STATUS = 5;
    private TeamApprovalAdapter mTeamApprovalAdapter = new TeamApprovalAdapter();
    private long mEventId = 0;
    private ArrayList<EventApproveUser> approveList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_approval);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setEnabled(false);
        RecyclerView approvalRecyclerView = findViewById(R.id.bbsList);
        approvalRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mTeamApprovalAdapter.setPassClickListener(position -> approveDialog(position, APPROVAL_STATUS));
        mTeamApprovalAdapter.setNotPassClickListener(position -> approveDialog(position, REJECTED_STATUS));
        approvalRecyclerView.setAdapter(mTeamApprovalAdapter);

        mEventId = getIntent().getLongExtra("event_id", 0L);
        approveList = getIntent().getParcelableArrayListExtra("approveList");
        mTeamApprovalAdapter.updateData(approveList);
    }

    private void approveDialog(int position, int status) {
        String message = "确定要审核通过吗？确认后不可修改";
        if (status == REJECTED_STATUS) {
            message = "确定要拒绝审核通过吗？确认后不可修改";
        }
        new AlertDialog.Builder(this).setTitle("审核").setMessage(message)
                .setPositiveButton("确认", (dialog, id) -> approveActivity(position, approveList.get(position).phoneNumber, status))
                .setNegativeButton("取消", null).show();
    }

    private void approveActivity(int position, String phoneNumber, int status) {
        MyHttpClient.approveActivity(new BiCiCallback(this) {
            @Override
            public void onResponseString(@NonNull String string) {
                if (status == APPROVAL_STATUS) {
                    approveList.get(position).status = "审核通过";
                } else {
                    approveList.get(position).status = "审核未通过";
                }
                runOnUiThread(() -> {
                    mTeamApprovalAdapter.updateData(approveList);
                });
            }
        }, mEventId, phoneNumber, status);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra("approveList", approveList);
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }
}